package com.kuanter.kuanterauto.activity;


import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.adapter.PopServiceAdapter;
import com.kuanter.kuanterauto.adapter.PopServiceTypeAdapter;
import com.kuanter.kuanterauto.adapter.PopSortAdapter;
import com.kuanter.kuanterauto.adapter.SitesAdapter;
import com.kuanter.kuanterauto.baseui.ProApplication;
import com.kuanter.kuanterauto.entity.KuanterService;
import com.kuanter.kuanterauto.entity.KuanterService.Service;
import com.kuanter.kuanterauto.entity.SiteInfo;
import com.kuanter.kuanterauto.view.PollListView;

/**
 * 网点列表
 * @author kuanter
 *
 */
public class SitesActivity extends Activity implements OnClickListener {
	RelativeLayout re_distanse_click;   //距离选择点击的layout
	RelativeLayout re_condiction_click;  //服务类型选择点击的layout
	RelativeLayout re_sort_click; 		//排序选择点击的layout
	
	PopupWindow mPopWindow_distance;		//距离选择后弹出的popwindow
	RadioButton radio1;			//可选择的距离
	RadioButton radio2;
	RadioButton radio3;
	RadioButton radio4;
	TextView chosed_distance;   //选择的距离的显示在页面
	ImageView distance_arrow;	//图片，上还是下
	
	PopupWindow mPopWindow_condition;   //服务选择后弹出的popwindow
	TextView chosed_service_type;		//选择的服务的显示
	ImageView condiction_arrow;			//图片，弹出pop后为上，pop消失后为下
	
	PopupWindow mPopWindow_sort;		//排序选择后弹出的popwindow
	TextView chosed_sort;			//选择的排序
	ImageView sort_arrow;			//图片，弹出pop后为上，pop消失后为下
	
	
	PollListView sites_list;	//网点列表listview
	
	int windowWidth;		//屏幕宽，popwindow占满屏幕宽度
	
	Button goto_map; //跳转到地图模式
	ProApplication application;
	SitesAdapter siteAdapter;
	
	public static final int POLLLIST = 50;
	
	public static List<SiteInfo> currentData;
	
	public static List<SiteInfo> getCurrentData() {
		return currentData;   //传递地图模式使用的
	}



	public static void setCurrentData(List<SiteInfo> currentData) {
		SitesActivity.currentData = currentData;
	}
	
	View loadMore;


	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == ProApplication.location_cuccess) {
				ProApplication.sort_default.clear();
//				ProApplication.sort_default = new TestData().addData(application.locData);  //要缓存的数据
				currentData = ProApplication.sort_default;//记录本页面的list数据
				siteAdapter.setSites(currentData);
//				List<SiteInfo> site = siteAdapter.getSites();
//				Log.i("aaa", "mmmm" + site);
				
				siteAdapter.notifyDataSetChanged();
			} else if(msg.what == POLLLIST) {
//				loadMore.setVisibility(View.VISIBLE);
					Log.i("bbb", "加载...");
			}
			super.handleMessage(msg);
		}
		
	};
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sites);
		application = (ProApplication) getApplication();
		//定位
		application.startRequest(mHandler);
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		windowWidth = dm.widthPixels;
		
		initView();
		
		
	}
	
	
	
	private void initView() {
		re_distanse_click = (RelativeLayout) findViewById(R.id.re_distanse_click);
		re_condiction_click = (RelativeLayout) findViewById(R.id.re_condiction_click);
		re_sort_click = (RelativeLayout) findViewById(R.id.re_sort_click);
		re_distanse_click.setOnClickListener(this);
		re_condiction_click.setOnClickListener(this);
		re_sort_click.setOnClickListener(this);
		
		chosed_distance = (TextView) findViewById(R.id.chose_distance);
		distance_arrow = (ImageView) findViewById(R.id.distance_arrow);
		
		condiction_arrow = (ImageView) findViewById(R.id.condiction_arrow);
		chosed_service_type = (TextView) findViewById(R.id.chose_service_type);
		
		chosed_sort = (TextView) findViewById(R.id.chose_sort);
		sort_arrow = (ImageView) findViewById(R.id.sort_arrow);
		
		goto_map = (Button) findViewById(R.id.goto_map);
		goto_map.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SitesActivity.this, MapSites.class);
				intent.putExtra("serviceName", chosed_service_type.getText().toString());
				startActivity(intent);
			}
		});
		
//		ProApplication.sort_default.add(site);
		
		sites_list = (PollListView) findViewById(R.id.list_all_site);
		sites_list.setHandler(mHandler);
		
		loadMore = getLayoutInflater().inflate(R.layout.progressbar, null);
		sites_list.addFooterView(loadMore);  //加载更多
		
		siteAdapter = new SitesAdapter(getLayoutInflater(),true);
		sites_list.setAdapter(siteAdapter);
		
		sites_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Intent intent = new Intent(SitesActivity.this, MerchantActivity.class);
//				long site_id = parent.getAdapter().getItemId(position);
				TextView tv_site = (TextView) view.findViewById(R.id.site_id);
				if(tv_site != null) {
					
					String site_id = tv_site.getText().toString();
					intent.putExtra("siteID", site_id);
					intent.putExtra("sitePosition", position);
					startActivity(intent);
				}
			}
		});
		
		
	}
	
	public void initRadio(View view) {
		radio1 = (RadioButton) view.findViewById(R.id.radio_1);
		radio2 = (RadioButton) view.findViewById(R.id.radio_2);
		radio3 = (RadioButton) view.findViewById(R.id.radio_3);
		radio4 = (RadioButton) view.findViewById(R.id.radio_4);
		
		radio1.setOnClickListener(this);
		radio2.setOnClickListener(this);
		radio3.setOnClickListener(this);
		radio4.setOnClickListener(this);
		
	}
	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.re_distanse_click:
				distance_arrow.setImageResource(R.drawable.ic_arrow_up_black);
				re_distanse_click.setSelected(true);
				re_condiction_click.setSelected(false);
				re_sort_click.setSelected(false);
				View mDistance = getLayoutInflater().inflate(
						R.layout.pop_distance, null);
				initRadio(mDistance);
				String chosed_distance_text = chosed_distance.getText()
						.toString();
				if (chosed_distance_text.equals(radio1.getText().toString())) {
					radio1.setChecked(true);
				} else if (chosed_distance_text.equals(radio2.getText()
						.toString())) {
					radio2.setChecked(true);
				} else if (chosed_distance_text.equals(radio3.getText()
						.toString())) {
					radio3.setChecked(true);
				} else if (chosed_distance_text.equals(radio4.getText()
						.toString())) {
					radio4.setChecked(true);
				}

				if (mPopWindow_distance == null) {
					mPopWindow_distance = new PopupWindow(mDistance,
							LayoutParams.WRAP_CONTENT,
							LayoutParams.WRAP_CONTENT);
					
					mPopWindow_distance.setWidth(windowWidth);
					
					mPopWindow_distance.setOutsideTouchable(true);
					 mPopWindow_distance.setFocusable(true);
					// 让pop可以点击外面消失掉
					mPopWindow_distance
							.setBackgroundDrawable(new ColorDrawable(0));

					mPopWindow_distance
							.setOnDismissListener(new OnDismissListener() {
								@Override
								public void onDismiss() {
									re_distanse_click.setSelected(false);
									distance_arrow
											.setImageResource(R.drawable.ic_arrow_down_black);
								}
							});
					
				} else {
					mPopWindow_distance.setContentView(mDistance);
				}
				mPopWindow_distance.showAsDropDown(re_distanse_click, 0, 0);
				mPopWindow_distance.update();
			
			break;
		
		case R.id.re_condiction_click:
			condiction_arrow.setImageResource(R.drawable.ic_arrow_up_black);
			re_condiction_click.setSelected(true);
			re_sort_click.setSelected(false);
			re_distanse_click.setSelected(false);
			
			View mCondition = getLayoutInflater().inflate(
					R.layout.pop_condiction, null);
			initCondition(mCondition);
			
			if (mPopWindow_condition == null) {
				mPopWindow_condition = new PopupWindow(mCondition,
						LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT);
				
				mPopWindow_condition.setOutsideTouchable(true);
				mPopWindow_condition.setFocusable(true);
				// 让pop可以点击外面消失掉
				mPopWindow_condition
						.setBackgroundDrawable(new ColorDrawable(0));

				mPopWindow_condition
						.setOnDismissListener(new OnDismissListener() {
							@Override
							public void onDismiss() {
								re_condiction_click.setSelected(false);
								condiction_arrow
										.setImageResource(R.drawable.ic_arrow_down_black);
							}
						});
			} else {
				mPopWindow_condition.setContentView(mCondition);
			}
			mPopWindow_condition.showAsDropDown(re_condiction_click, 0, 0);
			mPopWindow_condition.update();
			
			break;
			
		case R.id.re_sort_click:
			sort_arrow.setImageResource(R.drawable.ic_arrow_up_black);
			re_sort_click.setSelected(true);
			re_distanse_click.setSelected(false);
			re_condiction_click.setSelected(false);
			
			View mSort = getLayoutInflater().inflate(
					R.layout.pop_sort, null);
			initSort(mSort);
			
			if (mPopWindow_sort == null) {
				mPopWindow_sort = new PopupWindow(mSort,
						LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT);
				
				mPopWindow_sort.setWidth(windowWidth);
				
				mPopWindow_sort.setOutsideTouchable(true);
				mPopWindow_sort.setFocusable(true);
				// 让pop可以点击外面消失掉
				mPopWindow_sort
						.setBackgroundDrawable(new ColorDrawable(0));

				mPopWindow_sort
						.setOnDismissListener(new OnDismissListener() {
							@Override
							public void onDismiss() {
								re_sort_click.setSelected(false);
								sort_arrow
										.setImageResource(R.drawable.ic_arrow_down_black);
							}
						});
			} else {
				mPopWindow_sort.setContentView(mSort);
			}
			mPopWindow_sort.showAsDropDown(re_sort_click, 0, 0);
			mPopWindow_sort.update();
			break;
			
		case R.id.radio_1:
			chosed_distance.setText(radio1.getText().toString());
			if(mPopWindow_distance != null && mPopWindow_distance.isShowing()) {
				mPopWindow_distance.dismiss();
			}
			break;
			
		case R.id.radio_2:
			chosed_distance.setText(radio2.getText().toString());
			if(mPopWindow_distance != null && mPopWindow_distance.isShowing()) {
				mPopWindow_distance.dismiss();
			}
			break;
			
		case R.id.radio_3:
			chosed_distance.setText(radio3.getText().toString());
			if(mPopWindow_distance != null && mPopWindow_distance.isShowing()) {
				mPopWindow_distance.dismiss();
			}
			break;
			
		case R.id.radio_4:
			chosed_distance.setText(radio4.getText().toString());
			if(mPopWindow_distance != null && mPopWindow_distance.isShowing()) {
				mPopWindow_distance.dismiss();
			}
			break;

		default:
			break;
		}
		
	}



	private void initSort(View mSort) {
		String sortSelected = chosed_sort.getText().toString();
		ListView list_sort = (ListView) mSort.findViewById(R.id.list_sort);
		final PopSortAdapter sortAdapter = new PopSortAdapter(getLayoutInflater(),sortSelected);
		list_sort.setAdapter(sortAdapter);
		
		list_sort.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterview, View view,
					int i, long l) {
				Toast.makeText(SitesActivity.this, "刷新数据", 3000).show();
//				PopServiceAdapter.selected = i;
//				sortAdapter.notifyDataSetChanged();
				chosed_sort.setText(sortAdapter.getItem(i).toString());
				if(mPopWindow_sort != null) {
					mPopWindow_sort.dismiss();
				}
			}
		});
	}



	private void initCondition(View mCondition) {
		ListView list_service_type = (ListView) mCondition.findViewById(R.id.list_service_type);
		final ListView list_service = (ListView) mCondition.findViewById(R.id.list_service);
		list_service.setBackgroundResource(android.R.color.darker_gray);
//		List<KuanterService> kuanterService = KuanterService.getService();
		
		final PopServiceTypeAdapter serviceTypeAdapter = new PopServiceTypeAdapter(getLayoutInflater());
//		serviceTypeAdapter.setServiceData(kuanterService);
		list_service_type.setAdapter(serviceTypeAdapter);
		list_service_type.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
//				view.setSelected(true);
				PopServiceTypeAdapter.selected = position;
				serviceTypeAdapter.notifyDataSetChanged();
				KuanterService ks = (KuanterService) serviceTypeAdapter.getItem(position);
				List<Service> list = ks.getList_service();
				PopServiceAdapter serviceAdapter = new PopServiceAdapter(getLayoutInflater(),list);
				list_service.setAdapter(serviceAdapter);
			}
		});
		
		List<Service > pop_list = null;
		String tx_service = chosed_service_type.getText().toString();
	/*	for(int i = 0; i < kuanterService.size(); i++) {
			List<Service> list = kuanterService.get(i).getList_service();
			if(list != null) {
				if(list.get(i).getServiceName().contains(tx_service)) {
					PopServiceTypeAdapter.selected = i;
					pop_list = list;
					break;
				}
			}
		}*/
		PopServiceAdapter serviceAdapter = new PopServiceAdapter(getLayoutInflater(),pop_list);
		list_service.setAdapter(serviceAdapter);
		
		
		list_service.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				chosed_service_type.setText(parent.getAdapter().getItem(position).toString());
				if(mPopWindow_condition != null) {
					mPopWindow_condition.dismiss();
				}
			}
		});
	}



	//按下返回键
	public void exit(View view) {
		finish();
	}



	@Override
	protected void onResume() {
		//若在地图模式请求了数据这边要把数据添加进去
//		siteAdapter.setSites(currentData);
		super.onResume();
	}

	
	
	
}
